﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.Bliss.Domain.Post
{
    public class Wall
    {

        public IEnumerable<com.Bliss.Post> GetPosts(int companyId)
        {
            PostDbContext context = new PostDbContext();
            var postList = context.Posts
                .Where(
                    p => p.PostedBy.CompanyID == companyId
                        && p.ParentPostID.HasValue == false
                        && p.ToID.HasValue == false);
            return postList.AsEnumerable();
        }

        public IEnumerable<com.Bliss.Post> GetPrivatePosts(int userId)
        {
            PostDbContext context = new PostDbContext();
            var postList = context.Posts
                .Where(
                    p => p.ParentPostID.HasValue == false
                        && p.ToID.HasValue == true && (p.ToID.Value == userId || p.FromId == userId));
            return postList.AsEnumerable();
        }
        public IEnumerable<com.Bliss.Post> GetChildPosts(int parentID)
        {
            PostDbContext context = new PostDbContext();
            var postList = context.Posts
                .Where(p => p.ParentPostID == parentID);
            return postList.AsEnumerable();
        }
        public void SendPrivateMessage(int fromUserID, int? parentPostID, int? toUserID, string Message)
        {
            PostDbContext context = new PostDbContext();
            com.Bliss.Post post = new Bliss.Post();
            if (parentPostID.HasValue)
            {
                int? ToId = null;
                com.Bliss.Post lastPost = context.Posts.Where(p => p.ParentPostID == parentPostID || p.Id == parentPostID).OrderByDescending(o => o.SendDate).FirstOrDefault();
                if (lastPost == null)
                {
                    ToId = fromUserID;
                }
                else
                {
                    ToId = lastPost.FromId;
                }
                post.FromId = fromUserID;
                post.ParentPostID = parentPostID;
                post.Message = Message;
                post.ToID = ToId;
                post.SendDate = DateTime.Now;
            }
            else
            {
                post.FromId = fromUserID;
                post.ToID = toUserID;
                post.Message = Message;
                post.SendDate = DateTime.Now;
            }
            context.Posts.Add(post);
            context.SaveChanges();
        }


        public void Post(int fromUserID, int? parentPostID, string Message)
        {
            PostDbContext context = new PostDbContext();
            com.Bliss.Post post = new Bliss.Post();
            if (parentPostID.HasValue)
            {
                post.FromId = fromUserID;
                post.ParentPostID = parentPostID;
                post.Message = Message;
                post.SendDate = DateTime.Now;
            }
            else
            {
                post.FromId = fromUserID;
                post.Message = Message;
                post.SendDate = DateTime.Now;
            }
            context.Posts.Add(post);
            context.SaveChanges();
        }
        public void DeletePost(int postID)
        {
            PostDbContext context = new PostDbContext();
            List<com.Bliss.Post> lstPost = context.Posts.Where(p => p.Id == postID || p.ParentPostID == postID).ToList();
            lstPost.ForEach(p => context.Posts.Remove(p));
            context.SaveChanges();
        }
    }
}
